package com.bank.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.code.RespCode;
import com.bank.dao.OrganizationMapper;
import com.bank.entity.Organization;
import com.bank.entity.Subject;
import com.bank.exception.CommonException;
import com.bank.service.OrganizationService;


@Service(value="organizationService")
public class OrganizationServiceImpl implements OrganizationService {

	Logger logger = Logger.getLogger(OrganizationServiceImpl.class);
	
	@Autowired
	OrganizationMapper organizationMapper ;
	
	/***
	 *  新增机构信息
	 * @author shan
	 * 
	 */
	public void saveOrganization(Map<String, Object> params) throws CommonException {
		// TODO Auto-generated method stub
		String number = (String)params.get("number");
		Organization organization = this.getOrganizationByNumber(number);
		if(organization!= null){
			throw new CommonException(RespCode.ORGAN_IS_EXIST, "机构号已存在");
		}
		organization = new Organization();
		organization.setNumber(number);
		organization.setSenior_super((String)params.get("senior_super"));
		organization.setAccounting_super((String)params.get("accounting_super"));
		organization.setName((String)params.get("name"));
		organization.setLevel((String)params.get("level"));
		organization.setWorkdate((String)params.get("workdate"));
		organization.setAdress_code((String)params.get("adress_code"));
		organization.setStatus("1");
		organizationMapper.insertSelective(organization);
	}

	/***
	 * 更新机构信息
	 * @author shan 
	 * 
	 */
	public void updateOrganization(Map<String, Object> parmas) throws CommonException {
		String number = (String)parmas.get("number");
		Organization organization = this.getOrganizationByNumber(number);
		if(organization == null){
			throw new CommonException(RespCode.ORGAN_NOT_EXIST, "机构不存在");
		}
		String name = (String)parmas.get("name");
		String adressCode = (String)parmas.get("adress_code");
		String status = (String)parmas.get("status");
		String workdate = (String)parmas.get("workdate");
		if(name!=null){
			organization.setName(name);
		}
		if(adressCode!=null){
			logger.debug("adressCode"+adressCode);
			organization.setAdress_code(adressCode);
		}
		if(status!=null){
			organization.setStatus(status);
		}
		if(workdate!=null){
			organization.setWorkdate(workdate);
		}
		organizationMapper.updateByPrimaryKeySelective(organization);
		
	}

	/**
	 * @author shan
	 * @param 机构代号
	 * @return 机构信息
	 */
	public Organization getOrganizationByNumber(String number) {
		// TODO Auto-generated method stub
		return organizationMapper.selectByPrimaryKey(number);
	}
	
	/** 
	 * 
	 * 
	 */
	public List<Object> getOrganizationList(Map<String, Object> param) {
		List<Organization> orgList = organizationMapper.selectOrganizationtList(param);
		List<Object> objectList = new ArrayList<Object>();
		for(Organization org:orgList){
			objectList.add(org);
		}
		return objectList;
	}

}
